package cn.yjr.service.impl;

import cn.yjr.config.DynamicDataSource;
import cn.yjr.entity.Order;
import cn.yjr.mapper.order.OrderMapper;
import cn.yjr.mapper.storage.StorageMapper;
import cn.yjr.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {


    @Autowired
    OrderMapper orderMapper;
    @Autowired
    StorageMapper storageMapper;
    @Autowired
    DynamicDataSource dynamicDataSource;


    @Override
    public void addOrder(Order order) {
        orderMapper.createOrder(order);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void createOrder(Order order) throws FileNotFoundException {
        int res = storageMapper.decreaseStorage(order);
        log.info("[创建订单] 削减库存 ：{} 返回结果 {}", order.getCount(), res);
        orderMapper.createOrder(order);
        if (res == 0) {
            throw new FileNotFoundException("削减库存失败");
        }
        log.info("[创建订单] 创建订单 ：{} 返回结果 {}", order.getCount(), res);
    }
}
